package com.touzi.listeners;

import com.touzi.beans.dbs.quanqiufenhong.Meirijilu;
import com.touzi.beans.dbs.quanqiufenhong.QqfhJilu;
import com.touzi.beans.dbs.tongji.QqfhUser;
import com.touzi.beans.dbs.users.AddKeyByID;
import com.touzi.beans.dbs.yjtdfenhong.YJTDFenHong;
import com.touzi.mapper.QqfhMapper;
import com.touzi.mapper.TongjiMapper;
import com.touzi.mapper.UserMapper;
import com.touzi.mapper.YJTDFHMapper;
import com.touzi.proconfig.ProjectConfig;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;
import java.util.TimerTask;

/**
 * 永久推荐分红的计算任务
 */
public class YongiuTuijianFHTimerTask extends TimerTask {
    SqlSessionFactory sqlSessionFactory;
    Logger logger;
    //第一阶梯
    public static float fir = 0.02f;
    //第二阶梯
    public static float sec = 0.03f;
    //第三阶梯
    public static float thir = 0.05f;

    public YongiuTuijianFHTimerTask(SqlSessionFactory sqlSessionFactory) {
        this.sqlSessionFactory = sqlSessionFactory;
        logger = LoggerFactory.getLogger(this.getClass());
    }

    //永久推荐分红： 2.为符合条件的用户添加全球分红流水 3.增加用户的可提款额
    @Override
    public void run() {
        logger.info("开始永久团队分红");

        SqlSession sqlSession = sqlSessionFactory.openSession();
        TongjiMapper tongjiMapper = sqlSession.getMapper(TongjiMapper.class);
        QqfhMapper qqfhMapper=sqlSession.getMapper(QqfhMapper.class);
        YJTDFHMapper yjtdfhMapper=sqlSession.getMapper(YJTDFHMapper.class);
        UserMapper userMapper=sqlSession.getMapper(UserMapper.class);

        try {
            List<QqfhUser> firUsers = new ArrayList<>();
            List<QqfhUser> secUsers = new ArrayList<>();
            List<QqfhUser> thirUsers = new ArrayList<>();

            for (QqfhUser u : tongjiMapper.getAll1thYJFH()) {
                if (Float.parseFloat(tongjiMapper.getAUserAllTDYJ(u.yonghu)) >= 1000) {
                    firUsers.add(u);
                }
            }
            for (QqfhUser u : tongjiMapper.getAll2thYJFH()) {
                if (Float.parseFloat(tongjiMapper.getAUserAllTDYJ(u.yonghu)) >= 5000) {
                    secUsers.add(u);
                } else if (Float.parseFloat(tongjiMapper.getAUserAllTDYJ(u.yonghu)) >= 1000) { //如果大于20人小于5000的情况
                    firUsers.add(u);
                }
            }
            for (QqfhUser u : tongjiMapper.getAll3thYJFH()) {
                if (Float.parseFloat(tongjiMapper.getAUserAllTDYJ(u.yonghu)) >= 10000) {
                    thirUsers.add(u);
                } else if (Float.parseFloat(tongjiMapper.getAUserAllTDYJ(u.yonghu)) >= 5000) { //如果大于20人小于5000的情况
                    secUsers.add(u);
                } else if (Float.parseFloat(tongjiMapper.getAUserAllTDYJ(u.yonghu)) >= 1000) {
                    firUsers.add(u);
                }
            }
            //昨天的营业额
            String jinriyingshou=qqfhMapper.zuoriYingshou();
            if (jinriyingshou==null){
                logger.error("今天没有营收，无法分红");
                sqlSession.close();
                return;
            }
            BigDecimal jinryingshou=new BigDecimal(jinriyingshou);
            BigDecimal yijijiangjin=jinryingshou.multiply(new BigDecimal(fir)).setScale(2,RoundingMode.DOWN);
            BigDecimal erjijiangjin=jinryingshou.multiply(new BigDecimal(sec)).setScale(2,RoundingMode.DOWN);
            BigDecimal sanjijiangjin=jinryingshou.multiply(new BigDecimal(thir)).setScale(2,RoundingMode.DOWN);
            if (firUsers.size()>0){
                BigDecimal meirenfenhong=yijijiangjin.divide(new BigDecimal(firUsers.size()),2, RoundingMode.DOWN);
                AddKeyByID auserAddkeyong = new AddKeyByID();
                YJTDFenHong ayjtdFenHong =new YJTDFenHong();
                //添加记录和增加钱数
                for (QqfhUser u:firUsers){
                    ayjtdFenHong.yonghu=u.yonghu;
                    ayjtdFenHong.leixing=1;
                    ayjtdFenHong.jiner=meirenfenhong.floatValue();
                    yjtdfhMapper.insertNewReco(ayjtdFenHong);

                    auserAddkeyong.id=u.yonghu;
                    auserAddkeyong.keyong=meirenfenhong.floatValue();
                    userMapper.addKeyongByID(auserAddkeyong);
                }
            }
            if (secUsers.size()>0){
                BigDecimal meirenfenhong=erjijiangjin.divide(new BigDecimal(secUsers.size()),2, RoundingMode.DOWN);
                AddKeyByID auserAddkeyong = new AddKeyByID();
                YJTDFenHong ayjtdFenHong =new YJTDFenHong();
                //添加记录和增加钱数
                for (QqfhUser u:secUsers){
                    ayjtdFenHong.yonghu=u.yonghu;
                    ayjtdFenHong.leixing=2;
                    ayjtdFenHong.jiner=meirenfenhong.floatValue();
                    yjtdfhMapper.insertNewReco(ayjtdFenHong);

                    auserAddkeyong.id=u.yonghu;
                    auserAddkeyong.keyong=meirenfenhong.floatValue();
                    userMapper.addKeyongByID(auserAddkeyong);
                }
            }
            if (thirUsers.size()>0){
                BigDecimal meirenfenhong=sanjijiangjin.divide(new BigDecimal(thirUsers.size()),2, RoundingMode.DOWN);
                AddKeyByID auserAddkeyong = new AddKeyByID();
                YJTDFenHong ayjtdFenHong =new YJTDFenHong();
                //添加记录和增加钱数
                for (QqfhUser u:thirUsers){
                    ayjtdFenHong.yonghu=u.yonghu;
                    ayjtdFenHong.leixing=3;
                    ayjtdFenHong.jiner=meirenfenhong.floatValue();
                    yjtdfhMapper.insertNewReco(ayjtdFenHong);

                    auserAddkeyong.id=u.yonghu;
                    auserAddkeyong.keyong=meirenfenhong.floatValue();
                    userMapper.addKeyongByID(auserAddkeyong);
                }
            }
            sqlSession.commit();
            logger.info("1级用户"+firUsers.size()+" 平分"+yijijiangjin);
            logger.info("2级用户"+secUsers.size()+" 平分"+erjijiangjin);
            logger.info("3级用户"+thirUsers.size()+" 平分"+sanjijiangjin);
            logger.info("完成永久团队分红！");
        } catch (NumberFormatException e) {
            logger.error("分红出错了");
            sqlSession.close();
        } finally {
            sqlSession.close();
        }
    }
}
